package wta.cloud.service;
//神经网络
import Jama.Matrix;
public class NeuralNetwork {
	public static double NN(double[][] ddata){
		Matrix data    = new Matrix(ddata);
		Matrix meandata = new Matrix(new double[][] 
			 {{ 0,			44.7044, 	23.8903, 	118.9822, 	77.0757, 	5.9825, 	4.7008, 	142.2870, 
				41.9905, 	89.4759, 	30.3188, 	338.8169, 	12.5863, 	43.2488, 	216.7361, 	9.8012,
				12.4980, 	7.0747,		0.4196, 	3.4942, 	2.0652, 	57.8104, 	35.1128, 	24.3569,
				4.9783, 	63.3914, 	297.1485, 	4.9810, 	1.4712, 	5.1741, 	1.0213, 	6.1051}});
		Matrix stddata = new Matrix(new double[][]
			 {{ 1,		14.0738, 	3.8683,		18.7320,	12.4423, 	1.4588, 0.4796,		15.2382,
				4.0867, 5.2309, 	2.1498,		14.1435, 	0.9144, 	2.8345,	53.1951, 	1.0320,
				2.0785, 2.1801, 	0.1617, 	1.1454, 	0.5556, 	7.6945, 7.4220,		23.7678,
				1.2801,	16.3440,	87.4651,	0.9781, 	1.2584, 	1.2729, 0.0071, 	0.7017}});
		//Matrix c1      = new Matrix(new double[][] {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}});
		//Matrix c2      = new Matrix(new double[][] {{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}});
		Matrix w1 = new Matrix(new double[][] {
				{ 0.4796,-0.4888, 0.4426,-0.0304, 0.0110, 0.2188,-0.3550,-0.2352,-0.0031,-0.0114,-0.1405,-0.4125,-0.0327, 0.0644,-0.3248,-0.3591,-0.1552, 0.3450,-0.4137,-0.2712,-0.0836,-0.2544,-0.0744, 0.1473,-0.1013, 0.1863, 0.0230, 0.3061,-0.1512, 0.3486, 0.3881,-0.0312},
				{ 0.3883,-0.4766, 0.1128,-0.2537, 0.3476, 0.0815, 0.3697,-0.1388, 0.0526,-0.2069, 0.3348,-0.3913,-0.2584, 0.2097, 0.0625,-0.3031, 0.0776, 0.0457,-0.3176,-0.3556, 0.2036, 0.1072,-0.1030,-0.0905,-0.0667,-0.3908,-0.0711, 0.1664,-0.0347,-0.0467, 0.1416,-0.3873},
				{-0.1124,-0.4681,-0.1924,-0.1392, 0.2765,-0.2161,-0.2286,-0.3898,-0.1917, 0.4896, 0.2637,-0.0611,-0.3942,-0.2133, 0.2200, 0.1109,-0.4861, 0.3559,-0.0691, 0.0622, 0.2202, 0.3485, 0.1124,-0.4561,-0.2907, 0.3447, 0.3690, 0.2536, 0.3585, 0.0286,-0.0251,-0.2749},
				{-0.4242, 0.4710,-0.1149,-0.1278,-0.0943, 0.0912,-0.1942, 0.5745,-0.1292,-0.5125, 0.0749, 0.5651, 0.1482,-0.1153, 0.2040,-0.5847,-0.2759,-0.1569, 0.3383, 0.0516, 0.3054,-0.2673, 0.1020, 0.3064, 0.3814, 0.4899,-0.3956, 0.2702, 0.0076, 0.0004,-0.1289, 0.4816},
				{ 0.5931,-0.6065, 0.2321,-0.0397, 0.2564,-0.2004, 0.4299,-0.0429, 0.2464, 0.0285,-0.1003,-0.4711, 0.0094,-0.3119,-0.0163, 0.2895,-0.2612,-0.3969, 0.0415, 0.1752, 0.1892, 0.1263, 0.2265,-0.4596,-0.5436,-0.1486, 0.0833, 0.1207,-0.0904,-0.2721, 0.4954,-0.6346},
				{ 0.2239,-0.1416,-0.4512,-0.1156, 0.1176, 0.0473, 0.2408,-0.0445, 0.1401, 0.3437,-0.2090, 0.2103,-0.3987,-0.1618,-0.3515,-0.2764, 0.4173, 0.2733, 0.2989, 0.1688,-0.1049,-0.0130,-0.0732,-0.2929, 0.1209,-0.1654,-0.4595,-0.2293, 0.0355,-0.1175, 0.1013, 0.1996},
				{ 0.4200,-0.9574, 0.3238, 0.0672,-0.0496,-0.0755,-0.3120,-0.1021,-0.0322, 0.3600,-0.2380,-0.4837,-0.1875,-0.0753, 0.4442,-0.2232, 0.1054,-0.1037,-0.0002,-0.3698,-0.0611, 0.1326, 0.2376,-0.2058,-0.1965, 0.1396,-0.0497,-0.0101, 0.3600,-0.5944,-0.1100,-0.6448},
				{ 0.5873, 0.0105,-0.0253, 0.2799,-0.1633, 0.1124,-0.4130,-0.2632, 0.0541, 0.2242, 0.2147,-0.6815,-0.2813, 0.1473, 0.0075, 0.3351,-0.4619, 0.0881,-0.2261,-0.2743, 0.2805, 0.1893, 0.0536, 0.1171, 0.3034,-0.2168, 0.0093, 0.2234,-0.1468,-0.0402,-0.1247,-0.1230},
				{-0.3907,-0.0871, 0.3029,-0.0916,-0.1172, 0.2498, 0.3672,-0.0547, 0.2796,-0.0536, 0.1435,-0.4687, 0.0695, 0.1830, 0.3013,-0.0860,-0.3339, 0.1916,-0.0531, 0.0866,-0.2621,-0.1134,-0.2512, 0.1637, 0.1907, 0.1706,-0.0723, 0.0226, 0.2804, 0.2746,-0.2476,-0.0511},
				{-0.2774, 0.6611,-0.2508, 0.3046, 0.0731, 0.4662,-0.2345,-0.1400,-0.3436, 0.0499,-0.1540, 0.0320,-0.2827, 0.3321, 0.1404,-0.2711, 0.3773, 0.1073,-0.1461, 0.2314, 0.5014,-0.1822, 0.3049, 0.4637, 0.1760,-0.1889,-0.0107,-0.5011, 0.2278, 0.0565,-0.2780, 0.1862},
				{ 0.3369,-0.1573, 0.2654, 0.3249, 0.1435,-0.0107,-0.2444,-0.0325, 0.0290,-0.2436, 0.2438,-0.6013,-0.1265,-0.0169,-0.2650,-0.1520,-0.3098,-0.2724, 0.2399,-0.1287, 0.0496, 0.1851, 0.4198, 0.3170,-0.0017, 0.4101, 0.1012,-0.4112, 0.0923, 0.4139,-0.0364,-0.0451},
				{-0.5222,-0.0143,-0.1172, 0.0334,-0.1170, 0.4174, 0.1558, 0.2512,-0.4269, 0.0724, 0.1169, 0.0117, 0.1378,-0.0185,-0.1687,-0.1023, 0.1822,-0.2276,-0.3822,-0.3728, 0.1802, 0.3195, 0.2317,-0.3661, 0.0607,-0.4188,-0.4925,-0.1689,-0.4410,-0.3263, 0.0464, 0.3908}});
		Matrix b1 = new Matrix(new double[][]
				{{1.1968},{-0.6666},{0.4254},{0.0065},{0.9130},{0.6039},{-0.0429},{-1.4065},{0.9327},{-0.1087},{0.1651},{0.7659}});
		Matrix w2 = new Matrix(new double[][]
			   {{-0.6113, 0.2812, 0.4481, 0.4895,-0.6123, 0.0144,-0.0626,-0.0008,-0.4813, 0.4008,-0.2494,-0.3933},
				{ 0.0487,-0.4879, 0.3425,-0.0737, 0.4197,-0.4479, 0.4004,-0.2261, 0.5652,-0.2730, 0.6185,-0.4151},
				{ 0.5495, 0.1396, 0.6404,-0.5134, 0.6403,-0.4182, 0.1589, 0.2312, 0.0526,-0.0869, 0.2972,-0.2093},
				{ 0.0586, 0.3117, 0.2840,-0.7029, 0.9539,-0.5167, 0.9912, 0.6090, 0.1025,-0.6209,-0.2922,-0.3331},
				{-0.3850, 0.1385,-0.6059, 1.1859,-0.5461, 0.3164,-0.7507, 0.2223,-0.0052, 0.4229,-0.4077, 0.4969},
				{ 0.1551,-0.0258, 0.1106, 0.1231,-0.2844,-0.3292,-0.7993, 0.2203, 0.4806, 0.3829,-0.5864,-0.1776},
				{ 0.2875,-0.1155, 0.4068,-0.3651,-0.6495, 0.0844,-0.4663,-0.4649,-0.2465, 0.3482,-0.3167, 0.1738},
				{ 0.6218,-0.0384, 0.5999, 0.3520, 0.4265,-0.3969, 0.5127,-0.4303, 0.3183, 0.2775, 0.0563, 0.6303},
				{-0.1630,-0.6612,-0.3810, 0.8811,-0.2020, 0.4514,-0.7571,-0.6335, 0.2447, 0.7618,-0.1196, 0.2223},
				{ 0.4095,-0.1800, 0.2875,-0.1049,-0.4477,-0.1089,-0.4461,-0.1895,-0.3059,-0.5503, 0.2990, 0.5736},
				{ 0.0588,-0.0315, 0.0777,-0.4624, 0.0283, 0.3119, 0.8036, 0.3767, 0.1079,-0.3939, 0.1697, 0.3129},
				{ 0.3619,-0.2430, 0.5142, 0.5382, 0.4255,-0.5403,-0.3638, 0.5737, 0.1185, 0.3525,-0.0011, 0.5108}});
		Matrix b2 = new Matrix(new double[][]
				{{-0.1836},{-0.1188},{-0.9600},{-0.2364},{-0.0382},{-0.0763},{0.1532},{1.0854},{-0.4440},{-1.5080},{-0.3533},{-0.1169}});
		
		//Matrix softmaxTheta = new Matrix(new double[][]
		//	 {{-14.5950,  3.7845,  8.1850, -4.3066, -3.2575,  6.8851, -3.7333, 12.8411},
		//	  { 14.5917, -3.8005, -8.1872,  4.3091,  3.2598, -6.8817,  3.7220,-12.8437}});
		
		Matrix w3 = new Matrix(new double[][]
				{{0.9137,0.1626,-1.1403,-1.6075,1.5935,0.8863,0.6375,0.5581,1.4789,0.7177,-0.7021,0.1944}});
		Matrix b3 = new Matrix(new double[][]
				{{-1.6474}});
		
		
		
		
		
		Matrix norm_data = data.minus(meandata).arrayRightDivide(stddata);
	//System.out.println("norm_data");
	//norm_data.print(10,5);
		double[][] hiddenL1Activationarray = w1.times(norm_data.transpose()).plus(b1).getArray();
		int h1m = hiddenL1Activationarray.length;
		int h1n = hiddenL1Activationarray[0].length;
		for (int i = 0; i < h1m; i++) {
	        for (int j = 0; j < h1n; j++) {
	       	 hiddenL1Activationarray[i][j] = 1.0/(1.0+Math.exp(-hiddenL1Activationarray[i][j]));
	        }
		}
		Matrix hiddenL1Activation = new Matrix(hiddenL1Activationarray);
	//System.out.println("hiddenL1Activation");
	//hiddenL1Activation.print(10,5);
		
		double[][] hiddenL2Activationarray = w2.times(hiddenL1Activation).plus(b2).getArray();
		int h2m = hiddenL2Activationarray.length;
		int h2n = hiddenL2Activationarray[0].length;
		for (int i = 0; i < h2m; i++) {
	        for (int j = 0; j < h2n; j++) {
	       	 hiddenL2Activationarray[i][j] = 1.0/(1.0+Math.exp(-hiddenL2Activationarray[i][j]));
	        }
		}
		Matrix hiddenL2Activation = new Matrix(hiddenL2Activationarray);
	//System.out.println("hiddenL2Activation");
	//hiddenL2Activation.print(10,5);
		
	//方法1
	//	Matrix output = softmaxTheta.times(hiddenL2Activation);
	//System.out.println("output");
	//output.print(10,5);
	
	//方法2
		double[][] hiddenL3Activationarray = w3.times(hiddenL2Activation).plus(b3).getArray();
		int h3m = hiddenL3Activationarray.length;
		int h3n = hiddenL3Activationarray[0].length;
		for (int i = 0; i < h3m; i++) {
	        for (int j = 0; j < h3n; j++) {
	       	 hiddenL3Activationarray[i][j] = 1.0/(1.0+Math.exp(-hiddenL3Activationarray[i][j]));
	        }
		}
		Matrix hiddenL3Activation = new Matrix(hiddenL3Activationarray);
	//System.out.println("hiddenL3Activation");
	//hiddenL3Activation.print(10,5);	
		
		double p = hiddenL3Activation.get(0, 0);
		return p;
	}
	public static void main(String[] args) {
		 System.out.println("p=" + NN(new double[][] {{1,42,20.24,120,80,3.7,4.46,146,41.4,92.8,32.7,353,11.9,42.9,216,8.8,11,11.3,0.4,1.5,1.8,55,47.9,10,3.12,52.9,223.7,4.97,1.48,4.42,1.02,6}}) );
	}
}
